﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Util;
using SMEsCustomer360Degree.Core.Configuration;
using SMEsCustomer360Degree.Models.Models;
using SMEsCustomer360Degree.Service.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace SMEsCustomer360Degree.Service
{
    public class AuthenticationService : ServiceBase, IAuthentication
    {
        public ResponseRecord CheckLogin(UserRecord userRecord, HttpRequestBase Request = null)
        {
            ResponseRecord result = ResponseRecord.Default;
            if (userRecord == null)
            {
                result.IsSuccess = false;
                result.ErrorMessage = "User record to check is null";
                return result;
            }
            var isSupperAdmin = (userRecord.UserName.ToUpper() == SystemConfiguration.SUPPER_ADMIN.ToUpper());
            RetrieveUserByUserNameRequest request = new RetrieveUserByUserNameRequest
            {
                Header = new RequestHeaderRecord { ApplicationKey = userRecord.ApplicationKey, CallerName = "Authentication.Web.Service.AuthenticationService.CheckLogin" },
                UserName = userRecord.UserName
            };
            var response = RetrieveUserByUserName(request);

            //false when not Admin and not retrieve user recode
            if (!response.IsSuccess && !isSupperAdmin)
            {
                result.ErrorMessage = response.ErrorMessage;
                result.IsSuccess = response.IsSuccess;
                return result;
            }
            else
            {
                bool loginStatus = response.IsSuccess;
                var userLogIn = response.Record;
                if (userLogIn == null && !isSupperAdmin)
                {
                    result.IsSuccess = false;
                    result.ErrorMessage = @"The user name or password provided is incorrect";
                    return result;
                }

                // so sánh mật khẩu
                bool isAccessAuth = false;
                if (!isSupperAdmin)//Normal user in database
                {
                    string salt = userLogIn.Password;
                    isAccessAuth = (userLogIn.Active && salt == userRecord.Password.ToSHAPassword());
                }
                else //Sysadmin user config
                {
                    if (userLogIn == null)
                    {
                        //Insert supper admin record to db
                        RegisterUserRequest addUser = new RegisterUserRequest
                        {
                            Header = request.Header,
                            Record = new UserRecord
                            {
                                ApplicationKey = userRecord.ApplicationKey,
                                Email = userRecord.Email,
                                GroupIdentifier = null,
                                Password = "e03f4663da577be11bf0e00262b6e7396c70955690959877f39388d7a51541e8",
                                UserName = SystemConfiguration.SUPPER_ADMIN
                            }
                        };

                        var registerResponse = RegisterUser(addUser);
                        if (registerResponse.IsSuccess)
                        {
                            userLogIn = addUser.Record;
                        }
                    }
                    string salt = userLogIn.Password;
                    isAccessAuth = (salt == userRecord.Password.ToSHAPassword());
                }

                if (!isAccessAuth)
                {
                    result.IsSuccess = false;
                    result.ErrorMessage = @"The user name or password provided is incorrect";
                    return result;
                }


                // Don log with sysadmin
                if (Request != null && !isSupperAdmin)
                {
                    var ClientIPAddress = Request.UserHostAddress;
                    var ClientLogonUser = Request.LogonUserIdentity.Name;
                    var ComClientName = Request.UserHostName;
                    var LocalSystemUser = userLogIn.UserName;
                    var LocalSystemFullName = userLogIn.UserName;
                    var maxId = Provider.Users.GetMaxAccountLogonAuditID(userRecord.ApplicationKey, LocalSystemUser);
                    var userLogonAudit = new AccountLogonAudit
                    {
                        ApplicationKey = userRecord.ApplicationKey.ToString(),
                        Id = maxId + 1,
                        FullnameEnglish = LocalSystemFullName,
                        UserName = LocalSystemUser,
                        CreationDate = DateTime.Now,
                        ClientUserName = ClientLogonUser,
                        ClientMachineName = ComClientName,
                        ClientIpAddress = ClientIPAddress
                    };
                    Provider.Users.AddAccountLogonAudit(userLogonAudit);
                }
            }
            return result;
        }

        public RetrieveUserResponse RetrieveUserByUserName(RetrieveUserByUserNameRequest request)
        {
            RetrieveUserResponse response = new RetrieveUserResponse { IsSuccess = true, ErrorMessage = "" };
            if (request == null || request.Header == null || request.Header.ApplicationKey == null)
            {
                response.ErrorMessage = "Invalidate the request header";
                response.IsSuccess = false;
                return response;
            }

            var user = Provider.Users.RetrieveUser(request.Header.ApplicationKey.Value, request.UserName);
            if (user == null)
            {
                response.ErrorMessage = string.Format("Can not found user with ApplicationKey={0} in UserName={1}", request.Header.ApplicationKey, request.UserName);
                response.IsSuccess = false;
                return response;
            }

            UserRecord record = new UserRecord
            {
                ApplicationKey = new Guid(user.ApplicationKey),
                Password = user.Password,
                UserName = user.UserName,
                Active = user.IsActive,
                CreationDate = user.CreateDate,
                GroupIdentifier = user.GroupId,
                GroupName = user.Group == null ? null : user.Group.GroupNameEnglish
            };

            if (user.AccountInfo != null)
            {
                record.Email = user.AccountInfo.Email;
                record.FirstName = user.AccountInfo.FirstName;
                record.LastName = user.AccountInfo.LastName;
                record.Mobile = user.AccountInfo.MobilePin;
                record.ImageUrl = user.AccountInfo.ImageUrl;
                record.Birthday = user.AccountInfo.Birthday;
                record.Address = user.AccountInfo.Address;
                record.Gender = user.AccountInfo.Gender;
                //record.GroupIdentifier = user.GroupId;
            }
            response.Record = record;
            return response;
        }

        public ResponseRecord RegisterUser(RegisterUserRequest request)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            if (request == null || request.Record == null)
            {
                errorMessage = "Invalidate request";
                isSuccess = false;
            }
            else
            {
                //var application = Provider.Applications.RetrieveApplication(request.Record.ApplicationKey);
                //if (application == null)
                //{
                //    errorMessage = "Invalidate application request";
                //    isSuccess = false;
                //}
                //else
                //{
                Account record = new Account
                {
                    ApplicationKey = string.Empty,
                    Password = request.Record.Password,
                    UserName = request.Record.UserName,
                    CreateDate = DateTime.Now,
                    Active = BooleanString.TRUE,
                    AccountInfo = new AccountInfo
                    {
                        ApplicationKey = string.Empty,
                        UserName = request.Record.UserName,
                        Email = request.Record.Email,
                        FirstName = request.Record.FirstName,
                        LastName = request.Record.LastName,
                        MobilePin = request.Record.Mobile,
                        ImageUrl = request.Record.ImageUrl,
                        IsDeleted = BooleanString.FALSE,
                        IsAnonymous = BooleanString.FALSE,
                        IsApproved = BooleanString.TRUE,
                        IsLockedout = BooleanString.TRUE,
                        IsSuspended = BooleanString.FALSE,
                        CreateUtcDate = DateTime.UtcNow
                    }
                };

                var isAdded = Provider.Users.AddUser(record);
                if (!isAdded)
                {
                    errorMessage = "Can not insert supper admin user";
                    isSuccess = false;
                }
                //}
            }
            ResponseRecord response = new ResponseRecord
            {
                IsSuccess = isSuccess,
                ErrorMessage = errorMessage,
            };
            return response;
        }



    }
}
